import controllers.*;

import errors.*;

import flash.events.MouseEvent;

import models.*;

import mx.collections.ArrayCollection;
import mx.controls.Label;
import mx.core.DragSource;
import mx.events.DragEvent;
import mx.managers.DragManager;

import org.un.cava.birdeye.ravis.enhancedGraphLayout.event.VGNodeEvent;

import views.*;

private var _diagram:UMLDiagram;

public function drawDiagram(xml:XML) : void {
	ui_diagram_canvas.removeAllChildren();
	
	_diagram = new UMLDiagram();
	ui_diagram_canvas.addChild(_diagram);
	_diagram.init(xml);
	
	_diagram.dragDropHandler = function (event:DragEvent) : void {
		diagramManager.setClassInDiagram(event.dragSource.dataForFormat('umlClass') as UMLClass, event.localX, event.localY);
	}
	
	_diagram.nodeDragEndHandler = function (event:VGNodeEvent) : void {
		var umlClass:UMLClass = self().umlClassManager.getClassById(event.node.data.id);
		diagramManager.setClassInDiagram(umlClass, event.node.vnode.viewX + Math.floor(event.node.vnode.view.width / 2), event.node.vnode.viewY + Math.floor(event.node.vnode.view.height / 2));
	}
}

public function makeDiagramXML(diagram:XML) : XML {
	//Alert.show(diagram.toXMLString());
	var graph:XML = <graph/>;
	var edges:XML = <Edges/>;
	var nodes:XML =
	<Nodes>
		<Node id="0" name="graphe_master_node"  x="60" y="60"/>
	</Nodes>;
	
	if(diagram.umlclasses.length()) {
		var nodeID:int = 1;
		for each(var xmlClass:* in diagram.umlclasses.umlclass) {
			var umlClass:UMLClass = self().umlClassManager.getClassById(xmlClass.@oid);
			
			if(umlClass != null) {
				var xmlNode:XML;
				xmlNode = <Node />;
				xmlNode.@id = umlClass.oid;
				xmlNode.@name = umlClass.diagramLabel;
				xmlNode.@x = xmlClass.@x;
				xmlNode.@y = xmlClass.@y;
				
				if(umlClass.attributes.length) {
					for each(var umlAttribute:UMLAttribute in umlClass.attributes) {
						var xmlAttr:XML = <attribute />;
						xmlAttr.@name = umlAttribute.name;
						xmlAttr.@type = "";
						xmlAttr.@visibility = "";
						xmlNode.appendChild(xmlAttr); 
					}
				}
				
				nodes.appendChild(xmlNode);
				
				var parents:ArrayCollection = umlClass.parents;
				parents.addItemAt("0", 0);
				
				for(var oid:String in parents) {
					var xmlEdge:XML = <Edge/>;
					xmlEdge.@fromID = umlClass.oid;
					xmlEdge.@toID	= oid;
					xmlEdge.@line 	= "simple";
					if(oid != "0") {
						var parent:UMLClass = self().umlClassManager.getClassById(oid as int);
						if(parent != null) {
							if(parent.interfaceClass) {
								xmlEdge.@line 	= "dashed";
							}
						}
					}				
					edges.appendChild(xmlEdge);		
				}
				
				++ nodeID;
			}
			
		}
	}
	
	for each(var node:* in nodes.Node) {
		graph.appendChild(node);
	}
	
	for each(var edge:* in edges.Edge) {
		graph.appendChild(edge);
	}
	//Alert.show(graph.toXMLString());
	return graph;
}

public function diagramDrag(event:MouseEvent, umlClass:Object) : void {
	if(umlClass != null && umlClass is UMLClass) {
		// Get the drag initiator component from the event object.
	    var dragInitiator:* = event.target;
	
	    // Create a DragSource object.
	    var dragSource:DragSource = new DragSource();
	
	    // Add the data to the object.
	    dragSource.addData((umlClass as UMLClass), 'umlClass');
	
	    // Create a copy of the coin image to use as a drag proxy.
	    var dragProxy:Label = new Label();
	    dragProxy.text = (umlClass as UMLClass).name;
	
	    // Call the DragManager doDrag() method to start the drag. 
	    DragManager.doDrag(dragInitiator, dragSource, event, dragProxy, -10, -10, 0.8);	
	}
}